package com.game.springbootstart.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.springbootstart.VO.CommentVO;
import com.game.springbootstart.config.QueryPageParam;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.entity.Comments;
import com.game.springbootstart.service.ICommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static com.baomidou.mybatisplus.extension.toolkit.Db.page;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentsService commentsService;

    @GetMapping("/getName")
    public Result getUserName(@RequestParam Long userId){
        return Result.success(commentsService.getName(userId));
    }

    @GetMapping("/getGame")
    public Result getGame(@RequestParam Long gameId){
        return Result.success(commentsService.getGame(gameId));
    }

//    @PostMapping("/gameComments")
//    public Result getCommentsByGameId(@RequestParam Long gameId) {
//        return Result.success(commentsService.getCommentsByGameId(gameId));
//    }

    @DeleteMapping("/delete")
    public Result deleteComment(@RequestParam Long commentId) {
        return Result.success(commentsService.removeById(commentId));
    }

    @PostMapping("/listP")
    public Result listP(@RequestBody CommentVO commentVO) {
        int startRow = (commentVO.getPageNum() - 1) * commentVO.getPageSize();
        return Result.success(commentsService.getComments(commentVO,startRow));
    }

    @DeleteMapping("/bulkDelete")
    public Result bulkDelete(@RequestBody List<Long> ids) {
        return Result.success(commentsService.removeByIds(ids));
    }

    @PostMapping("/listPage")
    public Result listComment(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String content = (String)param.get("name");
        Page<Comments> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Comments::getContent,content);
        IPage<Comments> result = commentsService.page(page, queryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }
}
